package datastructure;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2019-11-04 下午3:42
 */
public class MyLinkedList {

    // 头节点指针
    private Node head;
    // 尾节点指针
    private Node last;
    // 链表的实际长度
    private int size;

    public static void main(String[] args) throws Exception {
        MyLinkedList linkedList = new MyLinkedList();
        linkedList.insert(3, 0);
        linkedList.insert(7, 1);
        linkedList.insert(9, 2);
        linkedList.insert(5, 3);
        linkedList.insert(6, 1);
        linkedList.remove(0);
        linkedList.output();

    }

    /**
     * 链表查找元素
     *
     * @param index
     * @return
     * @throws Exception
     */
    public Node get(int index) throws Exception {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("超出节点范围");
        }

        Node temp = head;
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }

        return temp;
    }

    /**
     * 链表插入元素
     *
     * @param data
     * @param index
     * @throws Exception
     */
    public void insert(int data, int index) throws Exception {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("超出节点范围");
        }

        Node insertNode = new Node(data);
        if (size == 0) {
            //空链表
            head = insertNode;
            last = insertNode;
        } else if (index == 0) {
            // 插入头部
            insertNode.next = head;
            head = insertNode;
        } else if (size == index) {
            //插入尾部
            last.next = insertNode;
            last = insertNode;
        } else {
            //插入中间节点
            Node prevNode = get(index - 1);
            insertNode.next = prevNode.next;
            prevNode.next = insertNode;
        }
        size++;
    }

    /**
     * 链表删除元素
     *
     * @param index
     * @return
     * @throws Exception
     */
    public Node remove(int index) throws Exception {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("超出节点范围");
        }
        Node removeNode = null;
        if (index == 0) {
            // 删除头结点
            removeNode = head;
            head = head.next;
        } else if (index == size - 1) {
            //删除尾节点
            final Node prevNode = get(index - 1);
            removeNode = prevNode.next;
            prevNode.next = null;
            last = prevNode;
        } else {
            // 删除中间节点
            final Node prevNode = get(index - 1);
            final Node nextNode = prevNode.next.next;
            removeNode = prevNode.next;
            prevNode.next = nextNode;
        }

        size--;
        return removeNode;
    }

    /**
     * 输出链表
     */
    public void output() {
        Node temp = head;
        while (temp != null) {
            System.out.println(temp.data);
            temp = temp.next;
        }
    }

    /**
     * 链表节点
     */
    private static class Node {
        int data;
        Node next;

        public Node(int data) {
            this.data = data;
        }
    }
}
